﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 标签操作一般处理程序  ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Wedn.Net.Common;
using Wedn.Net.IBLL;
using Wedn.Net.Model;
using Wedn.Net.UI.Handlers;
using Wedn.Net.Utility;

namespace Wedn.Net.Web.Handlers
{
    /// <summary>
    /// 标签操作一般处理程序
    /// </summary>
    public class TermAjax : AdminBaseHandler
    {
        private HttpContext _context;
        private readonly IBLLFactory _bllInstance = BLLFactory.BLLFactory.Instance;
        /// <summary>
        /// PR方法
        /// </summary>
        /// <param name="context"></param>
        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            _context = context;
            string action = RequestHelper.QueryString("action");
            switch (action)
            {
                //case "move_trash":
                //    MoveToTrash();
                //    break;
                case "load_category":
                    LoadTerm();
                    break;
                case "load_term":
                    LoadTerm();
                    break;
                case "insert_term":
                    InsertTerm();
                    break;
                case "update_term":
                    UpdateTerm();
                    break;
                case "delete_term":
                    DeleteTerm();
                    break;
            }
        }

        /// <summary>
        /// 加载标签信息
        /// </summary>
        private void LoadTerm()
        {
            int termType = RequestHelper.FormInt("termType");
            var list = _bllInstance.TermManager.Select(c => c.Type == termType).ToList();
            if (termType==(int)TermType.PostCategory)
            {
                IList<Term> categorys = new List<Term>();
                _bllInstance.TermManager.LoadTierCategoryList(list, categorys, "─", 0);
                var javaScript = new JavaScriptSerializer();
                ResponseStr(javaScript.Serialize(categorys));
            }else if (termType == (int) TermType.PostTag)
            {
                var javaScript = new JavaScriptSerializer();
                ResponseStr(javaScript.Serialize(list));
            }
        }

        /// <summary>
        /// 插入新标签
        /// </summary>
        private void InsertTerm()
        {
            if (RequestHelper.FormInt("termId") == 0)
            {
                var entity = new Term();
                entity.Name = RequestHelper.FormString("termName");
                entity.Slug = RequestHelper.FormString("termSlug").ToSlug();
                entity.Type = RequestHelper.FormInt("termType");
                entity.Count = 0;
                entity.Description = RequestHelper.FormString("termDescription");
                entity.ParentId = RequestHelper.FormInt("termParent");
                _bllInstance.TermManager.Insert(entity, true);
                ResponseStr(entity.Id != 0 ? string.Format("{{\"Status\":0,\"TermId\":{0}}}", entity.Id) : "{{\"Status\":1,\"Message\":\"添加失败，请稍后重试！\"}}");
            }
            else
                UpdateTerm();
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        private void UpdateTerm()
        {
            var entity = new Term();
            entity.Id = RequestHelper.FormInt("termId");
            entity.Name = RequestHelper.FormString("termName");
            entity.Slug = RequestHelper.FormString("termSlug").ToSlug();
            entity.Type = RequestHelper.FormInt("termType"); ;
            entity.Count = 0;
            entity.Description = RequestHelper.FormString("termDescription");
            entity.ParentId = RequestHelper.FormInt("termParent");
            int res = _bllInstance.TermManager.Update(entity, true);
            ResponseStr(res > 0 ? string.Format("{{\"Status\":0,\"TermId\":{0}}}", entity.Id) : "{{\"Status\":1,\"Message\":\"更新失败，请稍后重试！\"}}");
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        private void DeleteTerm()
        {
            var ids = RequestHelper.FormString("termId").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => i.ToInt()).ToArray();
            if (ids.Length > 0)
            {
                bool res = _bllInstance.TermManager.Delete(true, ids) == ids.Length;
                ResponseStr(res ? "success" : "error");
            }
        }

        ///// <summary>
        ///// 修改状态回收站
        ///// </summary>
        //private void MoveToTrash()
        //{
        //    string temp = RequestHelper.QueryString("ids");
        //    if (!string.IsNullOrEmpty(temp))
        //    {
        //        var intIds = temp.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => i.ToInt());
        //        var termIds = _bllInstance.TermManager.Select(t => intIds.Contains(t.Id)).Select(t => t.Id).ToArray();
        //        _bllInstance.TermManager.Delete(termIds);
        //        int res = _bllInstance.DbSaveChanges();
        //        ResponseStr(res == intIds.Count() ? "success" : "error");
        //    }
        //}
    }
}